package org.zjx;


import lombok.SneakyThrows;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.zjx.common.service.NpcService;
import org.zjx.common.service.PropService;
import org.zjx.common.service.RouteService;
import org.zjx.common.service.ScreenService;
import org.zjx.common.service.impl.NpcServiceImpl;
import org.zjx.common.service.impl.ScreenServiceImpl;
import org.zjx.prop.PropServiceImpl;
import org.zjx.route.RouteServiceImpl;

public class Main {
    @SneakyThrows
    public static void main(String[] args) {

        ApplicationConfig applicationConfig = new ApplicationConfig("sample-provider");
        applicationConfig.setSerializeCheckStatus("DISABLE");

        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(23134);
        protocolConfig.setSerialization("fastjson2"); // 强制使用FastJSON序列化

        RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);

        // 2. 暴露 UserService
        ServiceConfig<NpcService> userServiceConfig = new ServiceConfig<>();
        userServiceConfig.setInterface(NpcService.class);
        userServiceConfig.setRef(new NpcServiceImpl());
        userServiceConfig.setRegistry(registryConfig);
        userServiceConfig.setProtocol(protocolConfig);
        userServiceConfig.setApplication(applicationConfig);
        userServiceConfig.export();

        // 3. 暴露 OrderService
        ServiceConfig<ScreenService> orderServiceConfig = new ServiceConfig<>();
        orderServiceConfig.setInterface(ScreenService.class);
        orderServiceConfig.setRef(new ScreenServiceImpl());
        orderServiceConfig.setRegistry(registryConfig);
        orderServiceConfig.setProtocol(protocolConfig);
        orderServiceConfig.setApplication(applicationConfig);
        orderServiceConfig.export();


        // 3. 暴露 OrderService
        ServiceConfig<PropService> propServiceConfig = new ServiceConfig<>();
        propServiceConfig.setInterface(PropService.class);
        propServiceConfig.setRef(new PropServiceImpl());
        propServiceConfig.setRegistry(registryConfig);
        propServiceConfig.setProtocol(protocolConfig);
        propServiceConfig.setApplication(applicationConfig);
        propServiceConfig.export();



        // 3. 暴露 OrderService
        ServiceConfig<RouteService> serviceServiceConfig = new ServiceConfig<>();
        serviceServiceConfig.setInterface(RouteService.class);
        serviceServiceConfig.setRef(new RouteServiceImpl());
        serviceServiceConfig.setRegistry(registryConfig);
        serviceServiceConfig.setProtocol(protocolConfig);
        serviceServiceConfig.setApplication(applicationConfig);
        serviceServiceConfig.export();

        System.out.println("UserService,OrderService,PropService 和 RouteService 已暴露,使用序列化协议: " + protocolConfig.getSerialization());
        System.in.read(); // 阻塞主线程
    }
}